/////////////////////////////////////////////////////
//	File:		MainMenuState.cpp
//	Class:		SGP
//	Updated:	3/8/2012
//	Author:		Roc Myers
//	Purpose:	To contain all Main Menu related code
/////////////////////////////////////////////////////

#include "MainMenuState.h"
#include "Game.h"
#include "GamePlayState.h"
#include "OptionsState.h"
#include "HowToPlayState.h"
#include "CreditsState.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_DirectInput.h"
#include "SGD Wrappers\CSGD_XAudio2.h"
MainMenuState* MainMenuState::pInstance = nullptr;

MainMenuState::MainMenuState(void)
{
	CursorID		= -1;
	FontID			= -1;
	MusicID			= -1;
	TitleID			= -1;
	declineID		= -1;
	acceptID		= -1;
	errorID			= -1;
	moveID			= -1;
	BackgroundID	= -1;
	PanelID			= -1;
	Title			= true;
	Timer			= 0.0f;
	PlayBGM			= true;
	Map				= false;
	knightBmpID		= -1;
	wizardBmpID		= -1;
}

MainMenuState& MainMenuState::operator=(const MainMenuState&)
{
	return *this;
}

MainMenuState::~MainMenuState(void)
{
}

MainMenuState* MainMenuState::GetInstance(void)
{
	if(pInstance == nullptr)
		pInstance = new MainMenuState();

	return pInstance;
}

void MainMenuState::DeleteInstance()
{
	if(pInstance != nullptr)
	{
		delete pInstance;
		pInstance = nullptr;
	}
}

void MainMenuState::Enter(void)
{
	Timer = 0.0f;
	Selection = 0;
	Map = false;

	// Load in assets
	FontID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/LARPFont.png"));
	CursorID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/SGD_MenuCursor.png"));
	TitleID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/logo_game.png"));
	MusicID = CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("resource/Guild Wars Soundtrack- Guilds at war.xwm"));
	BackgroundID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/titlemenu.png"));
	Font.Initialize(FontID,"LARPFont.fnt");

	knightBmpID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/portrait_boxKnight.png"));
	wizardBmpID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/portrait_internetWizard.png"));

	// Set up sound
	CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(Game::GetInstance()->SfxVol);
	CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(Game::GetInstance()->BgmVol);
	CSGD_XAudio2::GetInstance()->MusicSetSongPan(MusicID, Game::GetInstance()->Pan);

	declineID	= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("resource/soundFX/GUI/GUI_Main_Decline.wav"));
	acceptID	= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("resource/soundFX/GUI/GUI_Main_Accept.wav"));
	errorID		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("resource/soundFX/GUI/GUI_Main_Error.wav"));
	moveID		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("resource/soundFX/GUI/GUI_Main_Move.wav"));

	PanelID		= CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/HUD.png"));

	arcadeEnter = false;
	arcadeEscape = false;
	arcadeUp = false;
	arcadeDown = false;
	arcadeLeft = false;
	arcadeRight = false;
	arcadeBackspace = false;
	arcadeShift = false;
	arcadePlus = false;
	arcadeMinus = false;
	arcadeP = false;

}

bool MainMenuState::Input(void)
{
	ArcadeInput();


	// Get menu input if the title screen isn't displaying
	if(!Title)
	{
		// Escape jumps the selection to Exit
		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE)||arcadeEscape)
			Selection = 4;

		// Pressing up and down switches between menu items
		// If at the top or bottom, it will wrap around
		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP)||arcadeUp)
		{
			CSGD_XAudio2::GetInstance()->SFXPlaySound(moveID);
			/*if(Map)
			{
				if(Selection > 0)
					Selection -= 1;
				else
					Selection = 2;
			}
			else*/
			{
				if(Selection > 0)
					Selection -= 1;
				else
					Selection = 4;
			}
		}

		if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN)||arcadeDown)
		{
			CSGD_XAudio2::GetInstance()->SFXPlaySound(moveID);
			/*if(Map)
			{
				if(Selection < 2)
					Selection += 1;
				else
					Selection = 0;
			}
			else*/
			{
				if(Selection < 4)
					Selection += 1;
				else
					Selection = 0;
			}
		}
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN)||arcadeEnter)
	{
		// If the title screen is up, switch to show the menu
		// Otherwise, change to the appropriate state
		if(Title)
			Title = false;
		else if(!Title && !Map)
		{
			switch(Selection)
			{
			case 0:
				// Map Select
				//Map = true;
				CSGD_XAudio2::GetInstance()->SFXPlaySound(acceptID);
				Game::GetInstance()->ChangeState(GamePlayState::GetInstance());
				break;
			
			case 1:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(acceptID);
				Game::GetInstance()->ChangeState(OptionsState::GetInstance());
				break;

			case 2:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(acceptID);
				Game::GetInstance()->ChangeState(HowToPlayState::GetInstance());
				break;

			case 3:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(acceptID);
				Game::GetInstance()->ChangeState(CreditsState::GetInstance());
				break;

			case 4:
				// Leaves the game
				CSGD_XAudio2::GetInstance()->SFXPlaySound(acceptID);
				Game::GetInstance()->ChangeState(nullptr);
				break;
			};
		}
		
	}
	return true;
}

void MainMenuState::Update(float fElapsedTime)
{
	Timer += fElapsedTime; 
	if(Timer > 1.0f)
		Timer = 0.0f;

	if(PlayBGM)
	{
		CSGD_XAudio2::GetInstance()->MusicPlaySong(MusicID, true);
		PlayBGM = false;
	}
}

void MainMenuState::Render(void)
{
	RECT GUItop = {6, 6, 212, 34};
	RECT GUImid = {6, 45, 212, 92};
	RECT GUIbot = {6, 103, 212, 132};

	
		// Display Title Screen or Menu
		CSGD_TextureManager::GetInstance()->Draw(BackgroundID,0,0,1.0f,1.0f);
		//CSGD_TextureManager::GetInstance()->Draw(TitleID,140,-120,1.0f,1.0f,nullptr,0.0f,0.0f,0.0f,D3DCOLOR_XRGB(255,255,255));

		CSGD_TextureManager::GetInstance()->Draw(knightBmpID,0,100,0.7f,0.7f);
		CSGD_TextureManager::GetInstance()->Draw(wizardBmpID,500,100,0.7f,0.7f);

		if(Title)
		{
			if(Timer > 0.5f)
				Font.Print("Press Enter", 250, 350, 2.0f);
		}
		else if(!Title && !Map)
		{
			CSGD_TextureManager::GetInstance()->Draw(PanelID, 195, 295, 2.0f, 2.0f, &GUItop, 0.0f, 0.0f, 0.0f, D3DCOLOR_ARGB(185,255,255,255));
			CSGD_TextureManager::GetInstance()->Draw(PanelID, 195, 351, 2.0f, 3.5f, &GUImid, 0.0f, 0.0f, 0.0f, D3DCOLOR_ARGB(185,255,255,255));
			CSGD_TextureManager::GetInstance()->Draw(PanelID, 195, 515, 2.0f, 3.0f, &GUIbot, 0.0f, 0.0f, 0.0f, D3DCOLOR_ARGB(185,255,255,255));
			Font.Print("Play", 255, 295,2.0f);
			Font.Print("Options", 255, 355,2.0f);
			Font.Print("How To Play", 255, 415,2.0f);
			Font.Print("Credits", 255, 475,2.0f);
			Font.Print("Exit", 255, 535,2.0f);

			switch(Selection)
			{
			case 0:
				CSGD_TextureManager::GetInstance()->Draw(CursorID, 235, 320);
				break;
		
			case 1:
				CSGD_TextureManager::GetInstance()->Draw(CursorID, 235, 380);
				break;

			case 2:
				CSGD_TextureManager::GetInstance()->Draw(CursorID, 235, 440);
				break;

			case 3:
				CSGD_TextureManager::GetInstance()->Draw(CursorID, 235, 500);
				break;

			case 4:
				CSGD_TextureManager::GetInstance()->Draw(CursorID, 235, 560);
				break;
			};
		}
		else if(!Title && Map)
		{
			
		}
	
}

void MainMenuState::Exit(void)
{
	CSGD_XAudio2* XA = CSGD_XAudio2::GetInstance();

	XA->MusicStopSong(MusicID);

	if(PanelID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(PanelID);
		PanelID = -1;
	}

	if(moveID != -1)
	{
		XA->SFXStopSound(moveID);
		XA->SFXUnloadSound(moveID);
		moveID = -1;
	}
	
	if(acceptID != -1)
	{
		XA->SFXStopSound(acceptID);
		XA->SFXUnloadSound(acceptID);
		acceptID = -1;
	}
	
	if(declineID != -1)
	{
		XA->SFXStopSound(declineID);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(declineID);
		declineID = -1;
	}
	
	if(errorID != -1)
	{
		XA->SFXStopSound(errorID);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(errorID);
		errorID = -1;
	}
	
	if(MusicID != -1)
	{
		CSGD_XAudio2::GetInstance()->MusicUnloadSong(MusicID);
		MusicID = -1;
	}
	
	if(CursorID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(CursorID);
		CursorID = -1;
	}
	
	if(FontID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(FontID);
		FontID = -1;
	}
	
	if(BackgroundID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(BackgroundID);
		BackgroundID = -1;
	}

	if(knightBmpID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(knightBmpID);
		knightBmpID = -1;
	}

	if(wizardBmpID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(wizardBmpID);
		wizardBmpID = -1;
	}

	DeleteInstance();
}

void MainMenuState::ArcadeInput()
{
	arcadeEnter = false;
	arcadeEscape = false;
	arcadeUp = false;
	arcadeDown = false;
	arcadeLeft = false;
	arcadeRight = false;
	arcadeBackspace = false;
	arcadeShift = false;
	arcadePlus = false;
	arcadeMinus = false;
	arcadeP = false;

	CSGD_DirectInput * di = CSGD_DirectInput::GetInstance();

	if(di->JoystickGetLStickDirPressed(DIR_UP,0))
	{
		arcadeUp = true;
	}

	if(di->JoystickGetLStickDirPressed(DIR_DOWN,0))
	{
		arcadeDown = true;
	}

	if(di->JoystickGetLStickDirPressed(DIR_LEFT,0))
	{
		arcadeLeft = true;
	}

	if(di->JoystickGetLStickDirPressed(DIR_RIGHT,0))
	{
		arcadeRight = true;
	}

	if(di->JoystickButtonPressed(0,0))
	{
		arcadeEnter = true;
	}

	if(di->JoystickButtonPressed(1,0))
	{
		arcadeBackspace = true;
	}

	if(di->JoystickButtonPressed(2,0))
	{
		arcadeP = true;
	}

	if(di->JoystickButtonPressed(3,0))
	{
		arcadeShift = true;
	}

	if(di->JoystickButtonPressed(4,0))
	{
		arcadePlus = true;
	}

	if(di->JoystickButtonPressed(5,0))
	{
		arcadeMinus = true;
	}

	if(di->JoystickButtonPressed(6,0))
	{
		arcadeEscape = true;
	}
}
